/**
 * author:Administrator
 * date:2013-5-19
 */
package com.roc.dao.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Repository;

import com.roc.bean.MenuBean;
import com.roc.dao.DaoException;
import com.roc.dao.IModuleDao;
import com.roc.pojo.Operators;
import com.roc.pojo.Module;

/**
 * @author Administrator
 *
 */
@Repository
public class ModuleDaoImpl extends BaseDaoImpl implements IModuleDao {

	@SuppressWarnings("unchecked")
	public List<MenuBean> listMenuByMerchantNo(Operators merchant)
			throws DaoException {
		List<MenuBean> menuList=new ArrayList<MenuBean>();
		try {
			String hql="select c.moduleCategoryCode,c.moduleCategoryName ,m.moduleCode,m.moduleName,m.moduleAct,c.displayIndex from ModuleCategory c,Module m,MerchantModuleRight r where c.moduleCategoryCode=m.categoryCode "
				+" and m.moduleCode=r.moduleCode and r.merchantNo=? and c.moduleCategoryCode like 'C%' "
				+" order by c.displayIndex asc,m.displayIndex asc ";
			Session session=sessionFactory.getCurrentSession();
			Query query=session.createQuery(hql);
			query.setString(0,merchant.getAccountNo());
			Iterator iterator=query.list().iterator();
			while(iterator.hasNext()){
				MenuBean bean=new MenuBean();
				Object[] obj=(Object[]) iterator.next();
				bean.setCategoryCode((String)obj[0]);
				bean.setCategoryName((String)obj[1]);
				bean.setModuleCode((String)obj[2]);
				bean.setModuleName((String)obj[3]);
				bean.setModuleAct((String)obj[4]);
				bean.setCategoryIndex((Long)obj[5]);
				menuList.add(bean);
				
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new DaoException("查询用户权限失败",e);
		}
		
		return menuList;
	}

	public List<MenuBean> listAllMenus(Operators merchant) throws DaoException {
		List<MenuBean> menuList=new ArrayList<MenuBean>();
		try {
			String hql="select c.moduleCategoryCode,c.moduleCategoryName ,m.moduleCode,m.moduleName,m.moduleAct,c.displayIndex from ModuleCategory c,Module m where c.moduleCategoryCode=m.categoryCode "
				
				+" order by c.displayIndex asc,m.displayIndex asc ";
			Session session=sessionFactory.getCurrentSession();
			Query query=session.createQuery(hql);
			//query.setString(0,merchant.getMerchantNo());
			Iterator iterator=query.list().iterator();
			while(iterator.hasNext()){
				MenuBean bean=new MenuBean();
				Object[] obj=(Object[]) iterator.next();
				bean.setCategoryCode((String)obj[0]);
				bean.setCategoryName((String)obj[1]);
				bean.setModuleCode((String)obj[2]);
				bean.setModuleName((String)obj[3]);
				bean.setModuleAct((String)obj[4]);
				bean.setCategoryIndex((Long)obj[5]);
				menuList.add(bean);
				
			}
		} catch (Exception e) {
			throw new DaoException("加载菜单失败",e);
		}
		
		return menuList;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Module> listAllModules(String merType) throws DaoException {
		List<Module> moduleList=new ArrayList<Module>();
		try {
			Session session=sessionFactory.getCurrentSession();
			Query query=session.createQuery("from Module t where t.roleType like '%"+merType+"%'");
			moduleList=query.list();
		} catch (Exception e) {
			throw new DaoException("查询模块信息出错",e);
		}
		return moduleList;
	}

	

}
